clear all 
capture log close

global data "/Users/ceweber/Dropbox/JPAM_TIV_code/original_data/"
global dta "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_files/"
global dataclean "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_output/"
global output "/Users/ceweber/Dropbox/JPAM_TIV_code/output/"

cd "$dataclean"
use  incidence_procanal_firstdiff.dta, clear
set more off

cd "$output"

***PERMUTATION TEST (FIGURE A.4 MANUFACTURER PORTION):
sort groupx weekly
gen week4202015=weekly==2875
gen week4202015diff=0
replace week4202015diff=week4202015-L.week4202015 if week4202015<.&L.week4202015<.
replace week4202015diff=week4202015-L2.week4202015 if week4202015<.&L2.week4202015<.&L.week4202015==.
replace week4202015diff=week4202015-L3.week4202015 if week4202015<.&L3.week4202015<.&L.week4202015==.&L2.week4202015==.
replace week4202015diff=week4202015-L4.week4202015 if L4.week4202015<.&L.week4202015==.&L2.week4202015==.&L3.week4202015==.

gen week4202016=weekly==2927
gen week4202016diff=0
replace week4202016diff=week4202016-L.week4202016 if week4202016<.&L.week4202016<.
replace week4202016diff=week4202016-L2.week4202016 if week4202016<.&L2.week4202016<.&L.week4202016==.
replace week4202016diff=week4202016-L3.week4202016 if week4202016<.&L3.week4202016<.&L.week4202016==.&L2.week4202016==.
replace week4202016diff=week4202016-L4.week4202016 if L4.week4202016<.&L.week4202016==.&L2.week4202016==.&L3.week4202016==.

gen week4202017=weekly==2979
gen week4202017diff=0
replace week4202017diff=week4202017-L.week4202017 if week4202017<.&L.week4202017<.
replace week4202017diff=week4202017-L2.week4202017 if week4202017<.&L2.week4202017<.&L.week4202017==.
replace week4202017diff=week4202017-L3.week4202017 if week4202017<.&L3.week4202017<.&L.week4202017==.&L2.week4202017==.
replace week4202017diff=week4202017-L4.week4202017 if L4.week4202017<.&L.week4202017==.&L2.week4202017==.&L3.week4202017==.

gen weekafter4202015=weekly==2876
gen weekafter4202015diff=0
replace weekafter4202015diff=weekafter4202015-L.weekafter4202015 if weekafter4202015<.&L.weekafter4202015<.
replace weekafter4202015diff=weekafter4202015-L2.weekafter4202015 if weekafter4202015<.&L2.weekafter4202015<.&L.weekafter4202015==.
replace weekafter4202015diff=weekafter4202015-L3.weekafter4202015 if weekafter4202015<.&L3.weekafter4202015<.&L.weekafter4202015==.&L2.weekafter4202015==.
replace weekafter4202015diff=weekafter4202015-L4.weekafter4202015 if L4.weekafter4202015<.&L.weekafter4202015==.&L2.weekafter4202015==.&L3.weekafter4202015==.

gen weekafter4202016=weekly==2928
gen weekafter4202016diff=0
replace weekafter4202016diff=weekafter4202016-L.weekafter4202016 if weekafter4202016<.&L.weekafter4202016<.
replace weekafter4202016diff=weekafter4202016-L2.weekafter4202016 if weekafter4202016<.&L2.weekafter4202016<.&L.weekafter4202016==.
replace weekafter4202016diff=weekafter4202016-L3.weekafter4202016 if weekafter4202016<.&L3.weekafter4202016<.&L.weekafter4202016==.&L2.weekafter4202016==.
replace weekafter4202016diff=weekafter4202016-L4.weekafter4202016 if L4.weekafter4202016<.&L.weekafter4202016==.&L2.weekafter4202016==.&L3.weekafter4202016==.

gen weekafter4202017=weekly==2980
gen weekafter4202017diff=0
replace weekafter4202017diff=weekafter4202017-L.weekafter4202017 if weekafter4202017<.&L.weekafter4202017<.
replace weekafter4202017diff=weekafter4202017-L2.weekafter4202017 if weekafter4202017<.&L2.weekafter4202017<.&L.weekafter4202017==.
replace weekafter4202017diff=weekafter4202017-L3.weekafter4202017 if weekafter4202017<.&L3.weekafter4202017<.&L.weekafter4202017==.&L2.weekafter4202017==.
replace weekafter4202017diff=weekafter4202017-L4.weekafter4202017 if L4.weekafter4202017<.&L.weekafter4202017==.&L2.weekafter4202017==.&L3.weekafter4202017==.

gen blackfriday2015=weekly==2907
gen blackfriday2015diff=0
replace blackfriday2015diff=blackfriday2015-L.blackfriday2015 if blackfriday2015<.&L.blackfriday2015<.
replace blackfriday2015diff=blackfriday2015-L2.blackfriday2015 if blackfriday2015<.&L2.blackfriday2015<.&L.blackfriday2015==.
replace blackfriday2015diff=blackfriday2015-L3.blackfriday2015 if blackfriday2015<.&L3.blackfriday2015<.&L.blackfriday2015==.&L2.blackfriday2015==.
replace blackfriday2015diff=blackfriday2015-L4.blackfriday2015 if L4.blackfriday2015<.&L.blackfriday2015==.&L2.blackfriday2015==.&L3.blackfriday2015==.

gen blackfriday2016=weekly==2959
gen blackfriday2016diff=0
replace blackfriday2016diff=blackfriday2016-L.blackfriday2016 if blackfriday2016<.&L.blackfriday2016<.
replace blackfriday2016diff=blackfriday2016-L2.blackfriday2016 if blackfriday2016<.&L2.blackfriday2016<.&L.blackfriday2016==.
replace blackfriday2016diff=blackfriday2016-L3.blackfriday2016 if blackfriday2016<.&L3.blackfriday2016<.&L.blackfriday2016==.&L2.blackfriday2016==.
replace blackfriday2016diff=blackfriday2016-L4.blackfriday2016 if L4.blackfriday2016<.&L.blackfriday2016==.&L2.blackfriday2016==.&L3.blackfriday2016==.

gen weekafterblackfriday2015=weekly==2908
gen weekafterblackfriday2015diff=0
replace weekafterblackfriday2015diff=weekafterblackfriday2015-L.weekafterblackfriday2015 if weekafterblackfriday2015<.&L.weekafterblackfriday2015<.
replace weekafterblackfriday2015diff=weekafterblackfriday2015-L2.weekafterblackfriday2015 if weekafterblackfriday2015<.&L2.weekafterblackfriday2015<.&L.weekafterblackfriday2015==.
replace weekafterblackfriday2015diff=weekafterblackfriday2015-L3.weekafterblackfriday2015 if weekafterblackfriday2015<.&L3.weekafterblackfriday2015<.&L.weekafterblackfriday2015==.&L2.weekafterblackfriday2015==.
replace weekafterblackfriday2015diff=weekafterblackfriday2015-L4.weekafterblackfriday2015 if L4.weekafterblackfriday2015<.&L.weekafterblackfriday2015==.&L2.weekafterblackfriday2015==.&L3.weekafterblackfriday2015==.

gen weekafterblackfriday2016=weekly==2960
gen weekafterblackfriday2016diff=0
replace weekafterblackfriday2016diff=weekafterblackfriday2016-L.weekafterblackfriday2016 if weekafterblackfriday2016<.&L.weekafterblackfriday2016<.
replace weekafterblackfriday2016diff=weekafterblackfriday2016-L2.weekafterblackfriday2016 if weekafterblackfriday2016<.&L2.weekafterblackfriday2016<.&L.weekafterblackfriday2016==.
replace weekafterblackfriday2016diff=weekafterblackfriday2016-L3.weekafterblackfriday2016 if weekafterblackfriday2016<.&L3.weekafterblackfriday2016<.&L.weekafterblackfriday2016==.&L2.weekafterblackfriday2016==.
replace weekafterblackfriday2016diff=weekafterblackfriday2016-L4.weekafterblackfriday2016 if L4.weekafterblackfriday2016<.&L.weekafterblackfriday2016==.&L2.weekafterblackfriday2016==.&L3.weekafterblackfriday2016==.

egen meandiff=mean(pricediff6) if taxdiff<., by(procloc weekly)

*try lots and lots of diff placebos:
set scheme s1manual
matrix estimatesfig=[.,.,.,.,.,.] 
set more off
*122
forvalues i=1(1)122{
quietly gen TaxChangemany=weekly>=2886-26+`i'
sort groupx weekly
quietly gen taxdiffmany=TaxChangemany-L.TaxChangemany
replace taxdiffmany=TaxChangemany-L2.TaxChangemany if TaxChangemany<.&L2.TaxChangemany<.&L.TaxChangemany==.
replace taxdiffmany=TaxChangemany-L3.TaxChangemany if TaxChangemany<.&L3.TaxChangemany<.&L.TaxChangemany==.&L2.TaxChangemany==.
replace taxdiffmany=TaxChangemany-L4.TaxChangemany if L4.TaxChangemany<.&L.TaxChangemany==.&L2.TaxChangemany==.&L3.TaxChangemany==.
quietly gen sellingweeks=(2886-26+`i')-firstsaledate
quietly gen sellinglastweek=lastsaledate-(2886-26+`i')
quietly gen runmany=weekly-(2886-26+`i')
  reghdfe pricediff6  taxdiffmany  i.difflength2  if abs(runmany)<=6&sellingweeks>=7&sellinglastweek>=7  [aw=weight6] , absorb(group_s##procloc##salesloc) vce(cluster procloc salesloc)
*could alternatively try to dummy out all of these, but doesn't work that well with compositional shifts and such: taxdiff week420*diff weekafter*diff blackfriday*diff
  local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=2886-26+`i'
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]
quietly drop TaxChangemany taxdiffmany runmany sellingweeks sellinglastweek
}
matrix list estimatesfig
mat2txt, matrix(estimatesfig) saving(procestimatesbyweek)   replace
import delimited "procestimatesbyweek.txt", encoding(ISO-8859-1) clear
rename c1 weekly
rename c2 estimate
*drop if weekly==2886|weekly==2875|weekly==2927
rename c3 cilb
rename c4 ciub
rename c5 se
tab estimate if weekly==2875|weekly==2876|weekly==2927|weekly==2928|weekly==2959|weekly==2960|weekly==2907|weekly==2908|weekly==2979|weekly==2980
*drop if weekly==2875|weekly==2876|weekly==2927|weekly==2928|weekly==2959|weekly==2960|weekly==2907|weekly==2908|weekly==2979|weekly==2980

*drop when n<5000:
drop if weekly<2871

*do a more symmetric drop for holidays:
*2015 4/20: 2875
tab estimate if abs(weekly-2875)<=2
drop if abs(weekly-2875)<=2
*2016 4/20: 2927
tab estimate if abs(weekly-2927)<=2
drop if abs(weekly-2927)<=2
*2017 4/20: 2979
tab estimate  if abs(weekly-2979)<=2
drop if abs(weekly-2979)<=2
*tax reform:
tab estimate if abs(weekly-2886)<=2&weekly!=2886 
drop if abs(weekly-2886)<=2&weekly!=2886 /*keep tax reform period for subsequent stuff*/
*2015 Black Friday: 2907 
tab estimate if abs(weekly-2907)<=2 
drop if abs(weekly-2907)<=2
*2016 Black Friday:  2959
tab estimate if abs(weekly-2959)<=2
drop if abs(weekly-2959)<=2

count if se==0
gen tstat=estimate/se
count if estimate<.
di 1/84 /*permutation test*/
twoway (hist estimate if weekly!=2886, start(-0.10) freq fcolor(gs12) lcolor(white)  width(.01)), ///
 xline(-0.072, lpattern(dash) lcolor(gs8)) graphregion(color(white)) bgcolor(white) xtitle({bf: Estimates}) ytitle({bf: Frequency}) title({bf: Manufacturer Permutation Test})
gr export  hist_procestpermutation.png, replace as(png)

cd "$dataclean"
use  incidence_procanal_firstdiff.dta, clear
set more off

cd "$output"
 
***TABLE 3: FIRST DIFF TABLE 
eststo tab2reg1: reghdfe pricediff6  taxdiff week420diff2 i.difflength2 if abs(run)<=6 [aw=weight6] , noabsorb vce(cluster procloc salesloc)
gen sample=e(sample)
summ centspct  if e(sample)==1&taxdiff==1  [aw=weight6]
estadd scalar centspct=r(mean)
scalar centspctx=r(mean)
test taxdiff=-centspctx
estadd scalar pval=r(p)
eststo tab2reg2: reghdfe pricediff6  taxdiff  week420diff2 i.difflength2 if abs(run)<=6 [aw=weight6] , absorb(group_s##procloc##salesloc) vce(cluster procloc salesloc)
summ centspct  if e(sample)==1&taxdiff==1  [aw=weight6]
estadd scalar centspct=r(mean)
scalar centspctx=r(mean)
test taxdiff=-centspctx
estadd scalar pval=r(p)
eststo tab2reg3: reghdfe pricediff6 taxdiff pricechange5  taxdiff2_pricechange5 week420diff2 i.difflength2 if abs(run)<=6 [aw=weight6] , absorb(group_s##procloc##salesloc  ) vce(cluster procloc salesloc)
summ centspct  if e(sample)==1&taxdiff==1  [aw=weight6]
estadd scalar centspct=r(mean)
scalar centspctx=r(mean)
test taxdiff=-centspctx
estadd scalar pval=r(p)
eststo tab2reg5: reghdfe pricediff4 taxdiff week420diff2 i.difflength2 if abs(run)<=6 [aw=weight6] , absorb(group_s##procloc##salesloc) vce(cluster procloc salesloc)
summ centsfortaxinvar2 if e(sample)==1&taxdiff==1  [aw=weight6]
estadd scalar centspct=r(mean)
scalar centspctx=r(mean)
test taxdiff=-centspctx
estadd scalar pval=r(p)
 

esttab tab2reg1 tab2reg2  tab2reg5     , keep(taxdiff*    ) star(* 0.05 ** 0.01 *** 0.001) se(%9.5f) stats(N N_clust1 centspct pricediff , fmt(0 0 3 3 3 3))
estout  tab2reg1 tab2reg2  tab2reg5        using proc_tab2.tex, cells(b(star fmt(3)) se(par)) replace ///
	   keep(taxdiff* ) style(tex)  starlevels(* 0.05 ** 0.01 *** 0.001)  stats(N N_clust1 centspct pricediff , fmt(0 0 3 3 3 3))

 eststo pltab2reg1: reghdfe pricediff6  placebodiff week420placebo i.difflength2  if abs(prun)<=6 [aw=weight6] , noabsorb vce(cluster procloc salesloc)
  eststo pltab2reg2: reghdfe pricediff6  placebodiff week420placebo i.difflength2  if abs(prun)<=6 [aw=weight6] , absorb(group_s##procloc##salesloc  ) vce(cluster procloc salesloc)
  *eststo pltab2reg3: reghdfe pricediff6  placebodiff2 week420diff2 i.difflength2   if abs(prun)<=6 [aw=weight6] , absorb(group_s##procloc##salesloc##weightbin2 ) vce(cluster procloc salesloc)
  eststo pltab2reg3: reghdfe pricediff6  placebodiff week420placebo i.difflength2  pricechange4 placebodiff2_pricechange4  if abs(prun)<=6 [aw=weight6] , absorb(group_s##procloc##salesloc   ) vce(cluster procloc salesloc)
 eststo pltab2reg5: reghdfe pricediff4  placebodiff week420placebo i.difflength2  if abs(prun)<=6 [aw=weight6] , absorb(group_s##procloc##salesloc  ) vce(cluster procloc salesloc)

 
 
esttab tab2reg1 tab2reg2  tab2reg5     , keep(taxdiff*   ) star(* 0.05 ** 0.01 *** 0.001) se(%9.5f) stats(N N_clust1 N_clust2 r2_a r2_a_within meanpre pctchange centspct pval, fmt(0 0 0 3 3 3 3))
esttab pltab2reg1 pltab2reg2 pltab2reg5    , keep(placebodiff*    ) star(* 0.05 ** 0.01 *** 0.001) se(%9.5f) stats(N N_clust1 N_clust2 r2_a r2_a_within meanpre pctchange, fmt(0 0 0 3 3 3 3))
 
 
estout  tab2reg1 tab2reg2  tab2reg5       using proc_tab2.tex, cells(b(star fmt(3)) se(par)) replace ///
	   keep(taxdiff* ) style(tex)  starlevels(* 0.05 ** 0.01 *** 0.001) stats(N N_clust1 N_clust2 r2_a meanpre pctchange centpct pval, fmt(0 0 0 3 3 3 3 3))
estout pltab2reg1 pltab2reg2 pltab2reg5     using proc_placebotab2.tex, cells(b(star fmt(3)) se(par)) replace ///
	   keep(placebodiff* ) style(tex)  starlevels(* 0.05 ** 0.01 *** 0.001) stats(N N_clust1 N_clust2 r2_a meanpre pctchange, fmt(0 0 0 3 3 3 3))

***TABLE 4: HETEROGENEITY TABLE:
eststo tabhetreg0: reghdfe pricediff6  taxdiff     week420diff2 i.difflength2 if abs(run)<=6   [aw=weight6] , absorb(procloc##salesloc##group_s) vce(cluster procloc salesloc)
gen samplereg=e(sample)
rename logpricedevprocpre_lv1os devprocpre_lv1os
foreach x in devprocpre_lv1os procretailsharepre {
summ `x' if samplereg==1 [aw=weight6]
gen std`x'=(`x'-r(mean))/r(sd)
gen taxdiff_std`x'=taxdiff*std`x'
}
gen taxdiff_priceroundwhole=taxdiff*priceroundwhole



eststo tabhetreg1: reghdfe pricediff6  taxdiff  taxdiff_priceroundwhole priceroundwhole  week420diff2 i.difflength2 if abs(run)<=6   [aw=weight6] , absorb(procloc##salesloc##group_s) vce(cluster procloc salesloc)
eststo tabhetreg2: reghdfe pricediff6  taxdiff  taxdiff_stddevprocpre_lv1os stddevprocpre_lv1os week420diff2 i.difflength2 if abs(run)<=6   [aw=weight6] , absorb(procloc##salesloc##group_s) vce(cluster procloc salesloc)
eststo tabhetreg3: reghdfe pricediff6  taxdiff  taxdiff_stdprocretailsharepre stdprocretailsharepre week420diff2 i.difflength2 if abs(run)<=6   [aw=weight6] , absorb(procloc##salesloc##group_s) vce(cluster procloc salesloc)
 eststo tabhetreg4: reghdfe pricediff6  taxdiff taxdiff_stddevprocpre_lv1os stddevprocpre_lv1os taxdiff_stdprocretailsharepre stdprocretailsharepre week420diff2 i.difflength2 if abs(run)<=6   [aw=weight6] , absorb(procloc##salesloc##group_s) vce(cluster procloc salesloc)
 
  esttab  tabhetreg1    tabhetreg2  tabhetreg3  tabhetreg4   ,  keep(taxdiff*)  star(* 0.1 ** 0.05 *** 0.01) se(%9.5f) stats(N N_clust1 centspct pricediff , fmt(0 0 3 3 3 3))
 estout  tabhetreg1    tabhetreg2  tabhetreg3  tabhetreg4     using proc_tabhet, cells(b(star fmt(3)) se(par)) replace ///
	   keep(taxdiff*  ) style(tex)  starlevels(* 0.1 ** 0.05 *** 0.01)  stats(N N_clust1 centspct pricediff , fmt(0 0 3 3 3 3))
 
 *repeat for placebo:
eststo pltabhetreg0: reghdfe pricediff6 placebodiff   week420placebo i.difflength2 if abs(prun)<=6   [aw=weight6] , absorb(procloc##salesloc##group_s) vce(cluster procloc salesloc)
drop samplereg
gen samplereg=e(sample)
rename logpricedevprocpl_lv1os devprocpl_lv1os
foreach x in devprocpl_lv1os procretailsharepl {
summ `x' if samplereg==1 [aw=weight6]
gen std`x'=(`x'-r(mean))/r(sd)
gen pdiff_std`x'=placebodiff*std`x'
}
gen pdiff_priceroundwhole=placebodiff*priceroundwhole


 eststo pltabhetreg1: reghdfe pricediff6  placebodiff  pdiff_priceroundwhole priceroundwhole  week420placebo i.difflength2 if abs(prun)<=6   [aw=weight6] , absorb(procloc##salesloc##group_s) vce(cluster procloc salesloc)
eststo pltabhetreg2: reghdfe pricediff6  placebodiff  pdiff_stddevprocpl_lv1os stddevprocpl_lv1os week420placebo i.difflength2 if abs(prun)<=6   [aw=weight6] , absorb(procloc##salesloc##group_s) vce(cluster procloc salesloc)
eststo pltabhetreg3: reghdfe pricediff6  placebodiff  pdiff_stdprocretailsharepl stdprocretailsharepl week420placebo i.difflength2 if abs(prun)<=6   [aw=weight6] , absorb(procloc##salesloc##group_s) vce(cluster procloc salesloc)
 eststo pltabhetreg4: reghdfe pricediff6  placebodiff pdiff_stddevprocpl_lv1os stddevprocpl_lv1os pdiff_stdprocretailsharepl stdprocretailsharepl week420placebo i.difflength2 if abs(prun)<=6   [aw=weight6] , absorb(procloc##salesloc##group_s) vce(cluster procloc salesloc)
 
  esttab  pltabhetreg1    pltabhetreg2  pltabhetreg3  pltabhetreg4   ,  keep(placebodiff* pdiff*)  star(* 0.1 ** 0.05 *** 0.01) se(%9.5f) stats(N N_clust1 centspct pricediff , fmt(0 0 3 3 3 3))
 estout  pltabhetreg1    pltabhetreg2  pltabhetreg3  pltabhetreg4    using proc_pltabhet, cells(b(star fmt(3)) se(par)) replace ///
	   keep(placebodiff*  ) style(tex)  starlevels(* 0.1 ** 0.05 *** 0.01)  stats(N N_clust1 centspct pricediff , fmt(0 0 3 3 3 3))
 

*COEFPLOTS WITH LEADS AND LAGS (FIGURE 3 - MANUFACTURER PORTION): 
rename L3Taxdiff L3taxdiff
rename L2Taxdiff L2taxdiff
rename LTaxdiff Ltaxdiff
set more off
eststo tab3reg1: reghdfe pricediff6  L3taxdiff2 L2taxdiff2 Ltaxdiff2  taxdiff2  Ftaxdiff2 F2taxdiff2 F3taxdiff2 L4group F4TaxChange  week420diff2 TaxChange   week420diff2 TaxChange i.difflength2  if  run>=-6&run<=10&difflength2<=4  [aw=weight6] , absorb(group_s##procloc##salesloc) vce(cluster procloc salesloc)
coefplot ,  graphregion(color(white)) bgcolor(white) xtitle({bf: Tax Reform Coefficients by Time}) ytitle({bf:First-Differenced Estimates})   title({bf:Manufacturer Event Study}) ///
    vertical yline(0, lpattern(dash) lcolor(black))  keep(L3taxdiff2 L2taxdiff2 Ltaxdiff2 taxdiff2 Ftaxdiff2 F2taxdiff2 F3taxdiff2) ///
	coeflabels(L3taxdiff2="t - 3" L2taxdiff2  ="t - 2" Ltaxdiff2= "t - 1" taxdiff2 = "t" Ftaxdiff2= "t + 1" F2taxdiff2="t + 2" F3taxdiff2="t + 3" )  
gr export  proc_pricediff.png, replace as(png)

*placebo version:
eststo tab3reg1: reghdfe pricediff6  L3placebodiff L2placebodiff Lplacebodiff  placebodiff2  Fplacebodiff F2placebodiff F3placebodiff L4placebogroup F4Placebo  week420placebo i.difflength2  if prun>=-6&prun<=10&difflength2<=4  [aw=weight6] , absorb(group_s##procloc##salesloc) vce(cluster procloc salesloc)
coefplot ,  graphregion(color(white)) bgcolor(white) xtitle({bf: Placebo Coefficients by Time}) ytitle({bf:First-Differenced Estimates})   title({bf:Manufacturer Event Study}) ///
    vertical yline(0, lpattern(dash) lcolor(black))  keep(L3placebodiff L2placebodiff Lplacebodiff placebodiff2 Fplacebodiff F2placebodiff F3placebodiff) ///
coeflabels(L3placebodiff="t - 3" L2placebodiff  ="t - 2" Lplacebodiff= "t - 1" placebodiff2 = "t" Fplacebodiff= "t + 1" F2placebodiff="t + 2" F3placebodiff="t + 3" ) 
gr export  proc_pricediffplacebo.png, replace as(png)
	

****HISTOGRAM (FIGURE 2 TOP PANEL):
eststo tab5reg1: reghdfe pricediff6 taxdiff   week420diff2 i.difflength2  if abs(run)<=6 [aw=weight6] , absorb(group_s##procloc##salesloc) vce(cluster procloc salesloc)
gen samplehist=e(sample)
twoway (hist pricediff6 if taxdiff==0&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1, start(-1.02) frac lcolor(gs12) fcolor(gs12) width(.04)) ///
 (hist pricediff6 if taxdiff==1&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1, start(-1.02) frac width(.04) fcolor(none) lcolor(green)), title({bf: Histogram of Weekly Manufacturer Price Changes}) xtitle({bf:Change in Log Weekly Manufacturer Price}) ytitle({bf: Fraction}) ///
 xline(0, lpattern(dash) lcolor(gs8)) graphregion(color(white)) bgcolor(white)  xline(-.198, lpattern(dash) lcolor(gs8)) xline(-.287, lpattern(shortdash) lcolor(gs12))  legend(label(1 "No Reform") label(2 "Reform") )
gr export  hist_procpricediff.png, replace as(png)
 
count if pricediff6<-.198 &taxdiff==1&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1
count if taxdiff==1&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1
di 361/1447

count if pricediff6<-.198 &taxdiff==0&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1
count if taxdiff==0&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1
di 971/7045

count if pricediff6>=-.198-.05&pricediff6<=-.198+.05 &taxdiff==1&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1
count if taxdiff==1&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1
di 202/1447

count if pricediff6>=-(.198/2)-.05&pricediff6<=-(.198/2)+.05 &taxdiff==1&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1
count if taxdiff==1&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1
di 357/1447

count if pricediff6>=-(.287/2)-.05&pricediff6<=-(.287/2)+.05 &taxdiff==1&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1
count if taxdiff==1&(pricediff6)<=1&pricediff6>=-1&abs(run)<=6&samplehist==1
di 274/1447

***TIME SERIES OF PRICE CHANGES:
egen meanpricechange=mean(abs(pricediff6)>.02&pricediff6<.) if samplehist==1, by(weekly)
egen meanpricechange1=mean(abs(pricediff6)>.04&pricediff6<.) if samplehist==1, by(weekly)
egen meanpricechange2=mean(abs(pricediff6)>.1&pricediff6<.) if samplehist==1, by(weekly)
egen meanpricechange3=mean(abs(pricediff6)) if samplehist==1, by(weekly)
summ meanpricechange*

sort weekly
graph twoway (line meanpricechange weekly if abs(run)<=6 , lcolor(black) lwidth(thick)), ///
	 title({bf: Time Series of Weekly Manufacturer Price Change Frequency}) ///
	 ytitle({bf: Fraction Price Change of More than 2%}, margin(medsmall))  xtitle({bf: Week}) xline(2885, lpattern(dash) lcolor(black)) xline(2875, lpattern(dash) lcolor(black))    ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) legend(region(lcolor(none))) legend(label(1 "Price Changes by Week") ) xlabel(#6, valuelabel  angle(forty_five))
gr export  figtseries_procpricediff.png, replace as(png)

gen two53=0 if samplehist==1&taxdiff==1
replace two53=1 if pricediff6>=-.198-.05&pricediff6<=-.198+.05& samplehist==1&taxdiff==1
gen half=0 if samplehist==1
replace half=1 if pricediff6>=-.2876/2-.05&pricediff6<=-.2876/2+.05& samplehist==1&taxdiff==1
summ two53 half if taxdiff==1
gen zerotaxdiff=0 if samplehist==1&taxdiff==1
replace zerotaxdiff=1 if abs(pricediff6)<.005&taxdiff==1
summ zerotaxdiff

***FIGURE 4 - DISPERSION FIGURE
 egen abslogpriceprocdev_lv1o=mean(abs(logpricedev_lv1o)), by(procloc salesloc group_s weekly)
 gen abslogpricedev_lv1o=abs(logpricedev_lv1o)
 egen tagtmp=tag(procloc salesloc group_s   weekly) if abslogpriceprocdev_lv1o<.
egen meanabsprocdev=mean(abslogpriceprocdev_lv1o) if tagtmp==1 , by(weekly)
egen tagw=tag(weekly) if meanabsprocdev<.
 twoway (scatter meanabsprocdev weekly if  abs(run)<=32&tagw==1,   mcolor(gs9) msymbol(circle_hollow) ) (lpoly meanabsprocdev weekly if tagw==1&run<0&abs(run)<=32, lcolor(black) lwidth(thick)  ) ///
 (lpoly meanabsprocdev weekly if tagw==1&run>=0&abs(run)<=32, lcolor(black) lwidth(thick)   ) , xline(2886, lcolor(black) lpattern(dash)) graphregion(color(white)) bgcolor(white) ///
 legend(region(lcolor(none))) legend(off) ytitle({bf: Average Dispersion (as percent) }, margin(medsmall))  xtitle({bf: Week})
  gr export  dispersionfig.png, replace as(png)
  
 reghdfe abslogpricedev_lv1o TaxChange##c.run if  abs(run)<=6 , vce(cluster procloc group_s) absorb(procloc group_s)
 
 **BANDWIDTH CHECKS FIGURE: (FIGURE A.5 - MANUFACTURER PORTION):
set more off
gr drop _all
matrix estimatesfig=[.,.,.,.,.,.] 
  
eststo fig1reg2: reghdfe pricediff6 taxdiff week420diff2 i.difflength2  if abs(run)<=2 [aw=weight6] , nocons absorb(group_s##procloc##salesloc ) vce(cluster procloc salesloc)
 local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]

eststo fig1reg3: reghdfe pricediff6 taxdiff week420diff2 i.difflength2  if abs(run)<=3 [aw=weight6] , nocons absorb(group_s##procloc##salesloc ) vce(cluster procloc salesloc)
 local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]
 
eststo fig1reg4: reghdfe pricediff6 taxdiff week420diff2 i.difflength2   if abs(run)<=4 [aw=weight6] , nocons absorb(group_s##procloc##salesloc ) vce(cluster procloc salesloc)
local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]
eststo fig1reg5: reghdfe pricediff6 taxdiff week420diff2 i.difflength2  if abs(run)<=5 [aw=weight6] , nocons absorb(group_s##procloc##salesloc ) vce(cluster procloc salesloc)
 local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]

eststo fig1reg6: reghdfe pricediff6 taxdiff week420diff2 i.difflength2  if abs(run)<=6 [aw=weight6] , nocons absorb(group_s##procloc##salesloc ) vce(cluster procloc salesloc)
 local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]

eststo fig1reg4: reghdfe pricediff6 taxdiff week420diff2 i.difflength2  if abs(run)<=7 [aw=weight6] , nocons absorb(group_s##procloc##salesloc ) vce(cluster procloc salesloc)
 local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]

eststo fig1reg4: reghdfe pricediff6 taxdiff week420diff2 i.difflength2  if abs(run)<=8 [aw=weight6] , nocons absorb(group_s##procloc##salesloc ) vce(cluster procloc salesloc)
 local n_y=e(N)
matrix beta = e(b)
	matrix vhat = e(V)
	estadd scalar months=0
	estadd scalar beta=beta[1,1]
	estadd scalar se=sqrt(vhat[1,1])
	estadd scalar cilb=beta[1,1]-1.96*sqrt(vhat[1,1])
	estadd scalar ciub=beta[1,1]+1.96*sqrt(vhat[1,1])
matrix estimatesfig=[estimatesfig\e(months), e(beta), e(cilb),e(ciub), e(se),  e(N) ]

matrix estimatesfig=estimatesfig[2...,1...]
*matrix rownames estimatesfig=    2015w27   2016w1 2016w14 2016w27 
matrix rownames estimatesfig=  "2 wks" "3 wks" "4 wks" "5 wks" "6 wks" "7 wks" "8 wks"  
*can add 4,3,2 if drop day of month dummies...but dom dummies matter for weight estimates, particularly when cut in weeks, not months (so non-symmetric days on each side of cutoff)
matrix list estimatesfig
graph drop _all
coefplot (matrix(estimatesfig[,2]), ci((estimatesfig[,3] estimatesfig[,4]))),  graphregion(color(white)) bgcolor(white) xtitle({bf: Bandwidth}) ytitle({bf:First-Differenced Price Estimate}) title({bf: Manufacturer Bandwidth Sensitivity })   vertical yline(0, lpattern(dash) lcolor(black))  
* title({bf: Tax Reform Estimates in First-Differences}) 
*gr combine `2'_bwchoicefig3 `2'_bwchoicefig5 `2'_bwchoicefig7, title({bf:`3'}) rows(1)  xsize(13) ysize(4.5) ycommon  graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
gr export  proc_firstdiffbandwidthfig.png, replace as(png)
